package com.cysion.study.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.KnowListAdapter
import com.cysion.study.entity.KnowledgeListEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_know_list.*

//习题与知识点
class KnowListActivity : BaseActivity() {
    override fun getLayoutId(): Int = R.layout.activity_know_list
    private lateinit var dataAdapter: KnowListAdapter
    private val dataList: MutableList<KnowledgeListEntity> = mutableListOf()
    private var curPage = 1

    private var courseId = ""

    override fun initView() {
        courseId = intent.extras.getString("courseId")
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("习题与知识点")
            setTexts("添加", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }else if (pos == TopBar.Pos.RIGHT) {
                    var bundle= Bundle()
                    bundle.putString("courseId", courseId)
                    UIHelper.jump(self, AddKnowActivity::class.java,bundle)//添加习题与知识

                    /*AlertSearch.search(self,""){ confirm: Int, start: String, end: String ->
                        CLog.i("@@@搜索="+start+end)
                        curPage=1
                        getDataList(curPage,start,end)
                    }*/
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
    }
    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList()
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList()
        }

    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        dataAdapter = KnowListAdapter(dataList, Box.context)
        rv_list.adapter = dataAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        dataAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == KnowListAdapter.preview) {//预览
               /* var bundle = Bundle()
                bundle.putString("orderId", obj.id)
                startActivity_ex<ClassRecordActivity>("key", bundle)*/
                startActivity_ex<AddKnowActivity>()//习题与知识
            }else if (flag == KnowListAdapter.del) {//删除
                ShowToast("删除")

            }else if (flag == KnowListAdapter.modify) {//编辑
                ShowToast("编辑")
                startActivity_ex<AddKnowActivity>()//添加习题与知识

            }else if (flag == KnowListAdapter.refuse) {//发布、取消
                ShowToast("发布取消")
            }
        }

        /*if (dataAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            dataList.addAll(lists)
            dataAdapter.notifyDataSetChanged()
        }*/
    }

    private fun getDataList() {
        Alert.loading(self)
        ApiClient.api.knowledgeList(courseId,"","",curPage.toString(),UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        dataAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }

    }

    private fun dowith(orderId: String, type: String, reason: String) {
        Alert.loading(self)
        ApiClient.api.dowith(UserCache.token, orderId, type, reason).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("操作成功")
                    dataAdapter.notifyDataSetChanged()
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }

    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }
    override fun closeMvp() {
    }

}
